﻿Public Class BigTypeAdd
    Public Event Updateparent()

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        of1.InitialDirectory = Util.GetCurrentFolderIcon
        of1.Filter = "ico|*.ico|bmp|*.bmp|png|*.png|gif|*.gif|all|*.*"
        of1.FilterIndex = 0

        of1.FileName = pb.Tag.ToString
        If of1.ShowDialog = Windows.Forms.DialogResult.OK Then
            pb.Image = Image.FromFile(of1.FileName).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            pb.Tag = of1.FileName
            If of1.FileName.ToLower.Contains(Util.GetCurrentFolderIcon.ToLower) Then
                Me.chk相对路径.Checked = True
            Else
                Me.chk相对路径.Checked = False
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If checkDb() = False Then
            Return
        End If

        软件类型.Id = txtId.Text
        软件类型.Name = txtName.Text.Trim
        软件类型.IconPath = pb.Tag.ToString
        If chk相对路径.Checked Then
            软件类型.IconpathType = "0"
            软件类型.IconPath = System.IO.Path.GetFileName(pb.Tag.ToString)
        Else
            软件类型.IconpathType = "1"
        End If

        Dim tyU As New XMLSoftWareBigType
        If Me.Text = "添加软件大分类" Then
            tyU.Create(软件类型)
        Else
            tyU.Update(软件类型)
        End If
        Dim fendir As String = System.IO.Path.Combine(Util.GetCurrentFolderSoftWare, 软件类型.Id)
        If Not System.IO.Directory.Exists(fendir) Then
            System.IO.Directory.CreateDirectory(fendir)
        End If
        If Me.Text = "添加软件大分类" Then
            RaiseEvent Updateparent()
            AddInit()
            Me.txtName.Focus()
            Return
        Else
            wys.util.Common.MessageBase.ShowMessage("更新成功")
        End If

        DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    Private _软件类型 As CSoftWareBigType = New CSoftWareBigType
    Public Property 软件类型() As CSoftWareBigType
        Get
            Return _软件类型
        End Get
        Set(ByVal value As CSoftWareBigType)
            _软件类型 = value
        End Set
    End Property

    Private Sub AddInit()
        Dim tey As New XMLSoftWareBigType
        Me.txtId.Text = tey.GetMaxid
        Me.txtName.Text = ""
        Me.pb.Image = Nothing
        Me.pb.Tag = ""
        Me.chk相对路径.Checked = False
    End Sub

    Private Sub UpdateInit()
        Dim tey As New XMLSoftWareBigType
        _软件类型 = tey.GetById(_软件类型.Id)
        Me.txtId.Text = _软件类型.Id
        Me.txtName.Text = _软件类型.Name
        Me.pb.Image = Image.FromFile(Util.GetCurrentFileIcon(_软件类型.IconPath, CInt(_软件类型.IconpathType))).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
        Me.pb.Tag = Util.GetCurrentFileIcon(_软件类型.IconPath, CInt(_软件类型.IconpathType))
        If _软件类型.IconpathType = "0" Then
            Me.chk相对路径.Checked = True
        Else
            Me.chk相对路径.Checked = False
        End If
    End Sub

    Private Sub TypeAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Me.Text = "添加软件大分类" Then
            AddInit()
        Else
            UpdateInit()
        End If

    End Sub

    Private Function checkDb() As Boolean

        If Me.txtName.Text.Trim = "" Then
            wys.util.Common.MessageBase.ShowMessage("名称不能不空")
            txtName.Focus()
            Return False
        End If

        If Not System.IO.File.Exists(pb.Tag.ToString) Then
            wys.util.Common.MessageBase.ShowMessage("图片路径不能为空")
            Me.btnSelect.Focus()
            Return False
        End If

        Return True

    End Function

    Public Sub New(ByVal 软件类型 As CSoftWareBigType)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _软件类型 = 软件类型
    End Sub
End Class
